/* 2016/3/27 */
package com.cosmos.pictureswitcher.io;

import android.os.Environment;

import java.io.File;

/**
 * Created by Leonardo on 2016/3/27.
 */
public class FileUtil {
    private static final String APPLICATION_DIR_NAME = "PictureSwitcher";

    /**
     * Checks if external storage is available for read and write
     */
    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /**
     * Checks if external storage is available to at least read
     */
    public static boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

    /**
     * Returns directory of this App in external storage.
     *
     * @return
     */
    public static File findAppDirectory() {
        return new File(Environment.getExternalStorageDirectory(),
                APPLICATION_DIR_NAME);
    }

    /**
     * Returns directory of specified list.
     *
     * @param listName
     * @return
     */
    public static File findPictureListDirectory(String listName) {
        return new File(findAppDirectory(), listName);
    }

    /**
     * Returns all picture files of specified list.
     * @param listName
     * @return
     */
    public static File[] findPictures(String listName) {
        File pictureListDir = findPictureListDirectory(listName);
        if (pictureListDir.exists()) {
            return pictureListDir.listFiles();
        } else {
            return new File[]{};
        }
    }
}
